/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Model.Lane;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;

/**
 *Creates main display panel for viewing of crossing model
 * @author Pro
 */
public class CarsDisplay extends JPanel {

    private Lane[] lanes;
    private boolean paused;
    private carsPaintTimer timer;

    class carsPaintTimer extends Thread {

        @Override
        public void run() {

            while (!paused) {

                repaint();
                try {

                    this.sleep(20);
                } catch (Exception e) {
                }
                System.out.println("Count");
            }
        }
    }

    /**
     *Constructor with one argument of Lane[] type
     * @param newLanes is of Lane[] type
     */
    public CarsDisplay(Lane[] newLanes) {

        lanes = newLanes;
        timer = new carsPaintTimer();
        setBackground(new Color(0x0fac2a));
        setPreferredSize(new Dimension(800, 800));
        timer.start();
    }

    //    this.View.CarsDisplay.carsPaintTimer.timer.start();
    // All Lanes should be drawn first as cars that change lane would disappear

    @Override
    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        for (int i = 0; i < lanes.length; i++) {
            lanes[i].draw(g);
        }

        for (int i = 0; i < lanes.length; i++) {

            for (int j = 0; j < lanes[i].getCars().size(); j++) {
                lanes[i].getCars().get(j).draw(g);
            }
        }

        for (int i = 0; i < lanes.length; i++) {
            lanes[i].getLights().draw(g);
        }
    }

//    public void addMessageListener(MessageListener task){
//
//    }
    /**
     * Public method that sets timers of this object to boolean value
     * @param paused is of boolean type
     */
    public void setPaused(boolean paused) {
        this.paused = paused;
    }

        /**
         *Public method that passes in reference of lanes object from crossing model
         * @param newLanes is of Lane[] type
         */
        public void setLanes(Lane[] newLanes) {

        this.lanes = newLanes;
    }

        /**
         *Public method that returns current operational state of timers
         * @return value is of boolean type
         */
        public boolean getPaused(){

        return paused;
    }

}